﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MinistryManager.DataAccess.Entities;
using MinistryManager.DataAccess.Services;
using MinistryManager.DataAccess.Services.NHibernate;
using Moq;
using NHibernate.Criterion;

namespace MinistryManager.DataAccess.Tests.ServiceTests
{
    [TestClass]
    public class VolunteerServiceTests
    {
        [TestMethod]
        public void RetrieveVolunteerWhenExists()
        {
            const string username = "ruben";
            const string pass = "pass";
            var dataModel = new Mock<IDataModel>();

            dataModel.Setup(dm =>
                dm.GetByCriterion<Volunteer>(
                    It.Is<List<ICriterion>>(l => l.Any(c => ((SimpleExpression)c).PropertyName == "Email" &&
                                                            ((SimpleExpression)c).Value.ToString() == username)
                                                 &&  l.Any(c => ((SimpleExpression)c).PropertyName == "Password" &&
                                                            ((SimpleExpression)c).Value.ToString() == pass))
                                                            ))
                .Returns(new List<Volunteer>() {new Volunteer(){Username = username}}).Verifiable("GetByCriterion was not called");

            var service = new VolunteerService(dataModel.Object);

            var volunteer = service.GetUserByUserNameAndPassword(username, pass);

            Assert.AreEqual(username, volunteer.Username,"Username should match");
            dataModel.VerifyAll();
        }

    }
}
